// 文件名编码方式为 7bit，导入模块还原 文件中文名称
const Buffer = require('buffer').Buffer
const uuid = require('uuid')
const db = require('../db/index')
const { sourcePath } = require('../config')

//
//
// 上传文件，包括图片、音频、
exports.uploadFile = (req, res) => {
  const { id: masterId } = req.user
  const { originalname, filename, path: path_, size, mimetype } = req.file
  const originalFilename = Buffer.from(originalname, 'binary').toString()

  //   const fixedUrl = (sourcePath + path_).replace(/\\/g, "/");
  const fixedUrl = path_.replace(/\\/g, '/')
  const obj = {
    id: uuid.v1(),
    filename,
    origin_name: originalFilename,
    upload_time: new Date(),
    master_id: masterId,
    type: mimetype,
    path: fixedUrl,
    size,
  }

  const sql = `insert into uploads set ?`
  db.query(sql, obj, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('上传文件失败！')
    return res.send({
      code: 200,
      message: '上传图片成功！',
      data: obj,
    })
  })
}

//
//
// 上传文件，包括图片、音频、
exports.uploadImageList = (req, res) => {
  const { id: masterId } = req.user
  const { groupId } = req.query

  //
  //
  // 判断桶是否存在
  if (groupId) {
    const sql2 = 'select * from groupes where id=? and master_id=?'
    db.query(sql2, [groupId, masterId], (err, results) => {
      if (err) return res.cc(err)
      if (results.length === 0) return res.cc('组不存在！上传图片到组中失败！')
    })
  }

  //
  // 将上传的图片记录到上传表中
  const insertData = req.files.map((item) => {
    const { originalname, filename, path: path_, size, mimetype } = item
    const originalFilename = Buffer.from(originalname, 'binary').toString()
    const fixedUrl = path_.replace(/\\/g, '/')

    return {
      id: uuid.v4(),
      filename,
      origin_name: originalFilename,
      upload_time: new Date(),
      master_id: masterId,
      type: mimetype,
      path: fixedUrl,
      size,
    }
  })
  const values = insertData.map((obj) => Object.values(obj))
  const placeholders = insertData
    .map(() => '(?, ?, ?, ?, ?, ?, ?, ?)')
    .join(',')

  const sql = `insert into uploads 
  (id, filename, origin_name, upload_time, master_id, type, path, size)
   VALUES ${placeholders}`

  db.query(sql, values.flat(), (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== insertData.length)
      return res.cc('批量上传文件失败！')
  })

  //
  //
  // 将上传完毕的图片写入到组中
  const insertData2 = insertData.map((item) => {
    return {
      id: uuid.v4(),
      master_id: masterId,
      group_id: groupId,
      filename: item.filename,
      path: item.path,
      upload_time: item.upload_time,
    }
  })
  const values2 = insertData2.map((obj) => Object.values(obj))
  const placeholders2 = insertData2.map(() => '(?, ?, ?, ?, ?, ?)').join(',')

  const sql2 = `insert into image 
  (id, master_id, group_id, filename, path, upload_time)
   VALUES ${placeholders2}`

  db.query(sql2, values2.flat(), (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== insertData2.length)
      return res.cc('批量上传图片到组中失败！')

    return res.cc('批量上传图片到组中成功！', 200)
  })
}
